﻿using System;
using System.Linq.Expressions;
using DapperLambdaSQL.Model;

namespace DapperLambdaSQL.BuildSQL
{
    public interface ISQLBuilder
    {
        /// <summary>
        /// SQL SELECT 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <returns>SELECT * FROM tableName;</returns>
        string Select<T>() where T : DbModel;

        /// <summary>
        /// SQL SELECT 语句 指定查询字段
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <param name="expression">查询字段表达式 eg:x =&gt; x.Id / x =&gt; new { x.Id, x.Name, ...}</param>
        /// <returns>SELECT column1,column2... FROM tableName;</returns>
        string Select<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModel;

        /// <summary>
        /// SQL SELECT DISTINCT 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">查询字段的类型</typeparam>
        /// <param name="expression">查询字段表达式 eg:x =&gt; x.Id / x =&gt; new { x.Id, x.Name, ...}</param>
        /// <returns>SELECT DISTINCT column1,column2... FROM tableName;</returns>
        string SelectDistinct<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModel;

        /// <summary>
        /// SQL WHERE 子句
        /// </summary>
        /// <typeparam name="T">数据库模型类</typeparam>
        /// <typeparam name="TElement">查询字段的类型</typeparam>
        /// <param name="expression">条件字段表达式 eg:x =&gt; x.Id</param>
        /// <param name="sqlOperator">操作符</param>
        /// <returns>fieldName = @fieldName / fieldName = value </returns>
        string Where<T, TElement>(Expression<Func<T, TElement>> expression, SQLOperator sqlOperator);

        /// <summary>
        /// 生成 ORDER BY 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">排序字段类型</typeparam>
        /// <param name="expression">ORDER BY表达式 eg:x =&gt; x.Id</param>
        /// <returns>"field ASC"</returns>
        string CreateOrderBy<T, TElement>(Expression<Func<T, TElement>> expression);

        /// <summary>
        /// 生成 ORDER BY 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">排序字段类型</typeparam>
        /// <param name="expression">ORDER BY表达式 eg:x =&gt; x.Id</param>
        /// <returns>"field DESC"</returns>
        string CreateOrderByDesc<T, TElement>(Expression<Func<T, TElement>> expression);

        /// <summary>
        /// 生成 INSERT 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">更新字段的类型</typeparam>
        /// <param name="expression">新增记录的表达式:x =&gt; new { x.Id, x.Name }</param>
        /// <returns></returns>
        string CreateInsert<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModel;

        /// <summary>
        /// 生成 UPDATE 字句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <typeparam name="TElement">更新字段的类型</typeparam>
        /// <param name="expression">更新字段的表达式 eg:x =&gt; new { x.Name, x.Remark }</param>
        /// <returns>"UPDATE 表名称 SET Name = @name,Remark = @Remark"</returns>
        string CreateUpdate<T, TElement>(Expression<Func<T, TElement>> expression) where T : DbModel;

        /// <summary>
        /// 生成 DELETE 语句
        /// </summary>
        /// <typeparam name="T">数据库模型类型</typeparam>
        /// <returns>"DELETE FROM 表名称"</returns>
        string CreateDelete<T>() where T : DbModel;

        /// <summary>
        /// 生成 指定读取行数 字句
        /// </summary>
        /// <param name="skipCount">跳过的行数</param>
        /// <param name="takeCount">读取的行数</param>
        /// <returns></returns>
        string CreatePageing(int skipCount, int takeCount);
    }
}